package org.vectrics.user.web.struts;

import java.util.Collection;
import java.util.HashSet;

import org.vectrics.SystemException;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Row;
import org.vectrics.user.Person;
import org.apache.log4j.Logger;
import org.vectrics.common.html.*;
import org.apache.ecs.html.A;
import org.vectrics.user.ContactInfo;
import org.vectrics.location.Address;

/**
 * @author mmoore
 */
public abstract class UserSelectTag extends UserGroupManagerTag {

	private static final long serialVersionUID = -7250957499581079000L;
	
	private transient static Logger log = Logger.getLogger(UserSelectTag.class);
	public static String SELECT_ATTRIBUTE_KEY = "userSelectHref";
	private String lastName = null;
	private String firstName = null;
	private String ssn = null;
	public static String NO_ELEMENTS_MESSAGE = "No Users Found";


	public UserSelectTag() {
		super();
	}

	protected boolean showNewUserLink() {
		return(true);
	}
	
	protected String getNoElementsMessage() {
		return(NO_ELEMENTS_MESSAGE);
	}
	

	protected Collection getUserPrivileges() {
		HashSet roles = new HashSet();
		roles.add("recruit-candidate");
		roles.add("recruit-admin");
		roles.add("recruit-employee");
		roles.add("recruit-recruiter");
		return(roles);
	}

	
	protected void setTableProperties(BorderTable table) {
		//
	}
    

	protected String getPageListLink() {
		return(getContextPath() + "/admin/user/index.do");
	}
    
	public void makeHeaderCells(Row row) {
		log.debug("makeHeaderCells() called.");
        row.setDefaultBgImage(this.getContextPath() + "/site-graphics/tbl_hdr.gif");
        
		Cell cell = row.addCell();
		cell.addElement(TextHelper.getFont("tableHdrFont", "<b>Last Name</b>"));
		
		cell = row.addCell();
		cell.addElement(TextHelper.getFont("tableHdrFont", "<b>First Name</b>"));
		
		cell = row.addCell();
		cell.addElement(TextHelper.getFont("tableHdrFont", "<b>User Name</b>"));
		
		cell = row.addCell();
		cell.addElement(TextHelper.getFont("tableHdrFont", "<b>Address</b>"));
		
		cell = row.addCell();
		cell.addElement(TextHelper.getFont("tableHdrFont", "<b>Phone</b>"));
		
		cell = row.addCell();
		cell.addElement(TextHelper.getFont("tableHdrFont", "<b>Options</b>"));
		
	}


	protected String getSelectHref(Person person)  {
		String href = (String)this.getRequest().getSessionAttribute(SELECT_ATTRIBUTE_KEY);
		if (href == null) {
			throw new SystemException("UserSelectTag requires session attribute (" 
					+ SELECT_ATTRIBUTE_KEY + ")" 
					+ " or overide getSelectHref(request, user) with derived UserSelectTag class.");
		}
				
		return(href);
	}
	
	
	
	protected void makeObjectRow(Row row, Person person) {
		Cell cell = row.addCell();
		cell.addElement(TextHelper.getFont("subFont", person.getLastName()));

		cell = row.addCell();
		cell.addElement(TextHelper.getFont("subFont", person.getFirstName() + " " 
					+ person.getMiddleName()));

		cell = row.addCell();
		if (person.getUser() != null)
			cell.addElement(TextHelper.getFont("subFont", person.getUser().getUserName()));
		
		log.debug("Show some contact info so user can verify that this is the "
				+ "person they want to select");
		ContactInfo contact = person.getContactInfo();
		String phone = "";
		String addressStr = "";
		if (contact != null) {
			if (contact.getHomePhone() != null)
				phone = contact.getHomePhone().getFormattedNumber();
			if (phone != null) {
				phone = "[H]" + phone;
			}
			Address address = contact.getHomeAddress();
			if (address == null) {
				log.debug("Home address is blank, try the work address");
				address = contact.getWorkAddress();
			}
			if (address != null) {
				addressStr = address.getAddress1() + ", " + address.getCity() + ", " + address.getPostalCode();
			}
		}
		cell = row.addCell();
		cell.addElement(TextHelper.getFont("subFont", addressStr));

		cell = row.addCell();
		cell.addElement(TextHelper.getFont("subFont", phone));
        
		cell = row.addCell();
		A editLink = new A();
		editLink.setHref(getSelectHref(person));
		editLink.setClass("subLink");
		editLink.addElement("select");
		cell.addElement(editLink);
	}


	protected String getDescription() {
		return("Select User");
	}


	public String getFirstName() {
		return firstName;
	}


	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}


	public String getLastName() {
		return lastName;
	}


	public void setLastName(String lastName) {
		this.lastName = lastName;
	}


	public String getSsn() {
		return ssn;
	}


	public void setSsn(String ssn) {
		this.ssn = ssn;
	}

}
